<?php
class Ahome {
	public static function main($funcode) {
		switch ($funcode) {
			case 1:
				sview::verify();
				self::__doindex();
				break;
			case 2:
				sview::verify(true, true);
				self::__myfeeds();
				break;
			case 3:
				sview::verify();
				self::__dotags();
				break;
			case 4:
				sview::verify();
				self::__doactivity();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//获取首页轮播图4-1-4
	private static function __doactivity() {
		$list  = model::load('live/activity')->get_banner(4, 0, 6);
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//获取网站首页信息4-1-1
	private static function __doindex() {
		$type   = intval(sview::$ctrl['type']);
		$tagid  = isset(sview::$ctrl['tagid'])? intval(sview::$ctrl['tagid']):0;
		$start  = intval(sview::$ctrl['offset']);
		$limit  = intval(sview::$ctrl['limit']);
		if ($limit < 1 || $limit > api_studio::LISTSIZE) {
			$limit = api_studio::LISTSIZE;
		}
		//设置常规的检索条件配置
		//$where = array('a.status'=>1, 'a.isshow'=>1);
		$where = array('a.status'=>1, 'a.testing'=>array(1, orm::OP_NE), 'a.isshow'=>array(0, orm::OP_NE));
		$field = api_studio::INDEXSTODUI.',(a.viewer+ 3*a.realuser) as vuser';
		$sorts = array();
		if ($type == 1) { //最新开播
			$where['a.olstatus'] = 1;
			$sorts['a.isapp']    = orm::DESC;  //优先APP端，然后PC端
			$sorts['a.neartime'] = orm::DESC;
			$sorts['vuser']      = orm::DESC;
			$sorts['a.id']       = orm::ASC;
		} else if ($type == 2) { //即将开播的数据
			$where['a.airtime']  = array(config::$nowtime, orm::OP_GT);
			$sorts['a.airtime']  = orm::ASC;
			$sorts['b.lv']       = orm::DESC;
			$sorts['a.id']       = orm::ASC;
		} else { //默认首页的热门数据
			$where[] = array('(');
			$where[] = array('a.olstatus', 1, orm::OP_EQ);
			$where[] = array('a.isvod', 1, orm::OP_EQ, orm::OP_OR);
			$where[] = array(')');
			$sorts['a.olstatus'] = orm::DESC; //在线
			$sorts['alstatus']   = orm::DESC; //预约
			$sorts['a.isapp']    = orm::DESC; //优先APP端，然后PC端
			$sorts['vuser']      = orm::DESC; //观众
			$sorts['lvstatus']   = orm::DESC; //等级类别
			$field .= ',if((a.olstatus <> 1 && (cast(a.airtime as SIGNED)-UNIX_TIMESTAMP()) > 0), 1, 0) as alstatus,if((b.starlv = 4), 0, 1) as lvstatus';
		}
		if ($tagid > 0) {
			$stuids = api_studio::getbytagid($tagid);
			if ($stuids) {
				$where[] = array('a.id', $stuids, orm::OP_IN);
			} else {
				sview::$view->assign('list', array());
				sview::$view->display();
				return;
			}
		}
		$fsids = isset(sview::$ctrl['ids'])? sview::$ctrl['ids']:null;
		if ($fsids) {
			$fsids = explode(',', trim($fsids, ','));
			$fsids = array_filter($fsids, 'is_numeric');
			$fsids = array_unique($fsids);
			if (is_array($fsids) && count($fsids) > 0) {
				$where[] = array('a.id', $fsids, orm::OP_NOTIN);
				$start         = 0;
			}
		}
		//遍历获取直播间的数据资料
		$list  = model::load('live/studio')->index_list($start, $limit, $where, $field, $sorts);
		$uorm  = model::load('live/userinfo');
		//获取一定订阅的资料信息
		$feed  = array();
		$acl   = acl::getinst('vedio');
		if ($acl->user) {
			$userid = $acl->user['id'];
			$feed   = api_studio::feed($userid);
		}
		foreach ($list as &$item) {
			$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
			$lv    = api_lv::item($user['lv'], 0);
			$item['nickname'] = htmlspecialchars_decode($user['nickname']);
			$item['avtar']    = $user['thumb'];
			$item['lvicon']   = $lv['iconsrc'];
			if ($item['airtime'] < config::$nowtime) {
				$item['airtime'] = 0;
			}
			$item['lv']       = $user['lv'];
			$item['iscurated']= api_studio::grade($item['starlv'], $item['iscurated']);
			$item['tags']     = empty($item['tags'])? array():json_decode($item['tags'], true);
			$item['studioid'] = $item['id'];
			$item['isfeed']   = isset($feed[$item['studioid']])? 1:0;
			$item['title']    = stripslashes($item['title']);
			$item['viewer']   = api_notify::oluser($item['id'], $item);
			unset($item['author'], $item['id'], $item['lasttime'], $item['studiono'], $item['exp'], $item['charms']);
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//获取首页我的关注数据信息4-1-2
	private static function __myfeeds() {
		$tagid  = isset(sview::$ctrl['tagid'])? intval(sview::$ctrl['tagid']):0;
		$start  = intval(sview::$ctrl['offset']);
		$userid = acl::getinst('vedio')->user['id'];
		$limit  = intval(sview::$ctrl['limit']);
		if ($limit < 1 || $limit > api_studio::LISTSIZE) {
			$limit = api_studio::LISTSIZE;
		}
		//查询我的关注或者指定标签下面的我的关注
		$stuid  = array();
		$where  = array('userid'=>$userid, 'focus'=>1);
		$fansid = model::load('live/fans')->set_devision($userid)->get_only_column(0, -1, $where, 'fansid');
		if (count($fansid) > 0) {
			if ($tagid > 0) {
				$stuid = api_studio::getbytagid($tagid);
				if (empty($stuid)) {//不存在标签的话返回空的
					$stuid = array(0);
				}
			}
		}
		$list   = array();
		$feed   = api_studio::feed($userid);
		//获取真实要获取的数据信息
		if (count($fansid) > 0) {
			$where = array('a.userid'=>array($fansid, orm::OP_IN), 'a.isshow'=>1, 'a.status'=>1);
			if ($stuid) {
				$where[] = array('a.id', $stuid, orm::OP_IN);
			}
			$fsids = input::get_post('ids');
			if ($fsids) {
				$fsids = explode(',', trim($fsids, ','));
				$fsids = array_filter($fsids, 'is_numeric');
				$fsids = array_unique($fsids);
				if (is_array($fsids) && count($fsids) > 0) {
					$where[] = array('a.id', $fsids, orm::OP_NOTIN);
					$start         = 0;
				}
			}
			$sorts = array('a.olstatus'=>orm::DESC, 'a.viewer'=>orm::DESC);
			$list  = model::load('live/studio')->index_list($start, $limit, $where, api_studio::INDEXSTODUI, $sorts);
			$uorm  = model::load('live/userinfo');
			foreach ($list as &$item) {
				$user  = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$lv    = api_lv::item($user['lv'], 0);
				$item['nickname'] = htmlspecialchars_decode($user['nickname']);
				$item['avtar']    = $user['thumb'];
				$item['lvicon']   = $lv['iconsrc'];
				$item['lv']       = $user['lv'];
				$item['tags']     = empty($item['tags'])? array():json_decode($item['tags'], true);
				if ($item['airtime'] < config::$nowtime) {
					$item['airtime'] = 0;
				}
				$item['studioid'] = $item['id'];
				$item['isfeed']   = isset($feed[$item['id']])? 1 : 0;
				$item['iscurated']= api_studio::grade($item['starlv'], $item['iscurated']);
				$item['title']    = stripslashes($item['title']);
				$item['viewer']   = api_notify::oluser($item['id'], $item);
				unset($item['author'], $item['id'], $item['lasttime'], $item['studiono'], $item['exp'], $item['charms']);
			}
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//直播分类列表4-1-3
	private static function __dotags() {
		$where = array('issys'=>1, 'status'=>1, 'isshow'=>1);
		$list  = model::load('live/tags')->get_only_list(0, -1, $where, 'id,name,icon,thumb', 'rank', orm::DESC);
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
}